<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 事务传播行为详解 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .drop-cap::first-letter {
            font-size: 3.5rem;
            line-height: 3.5rem;
            float: left;
            margin-right: 0.5rem;
            margin-bottom: -0.5rem;
            color: #4f46e5;
            font-weight: bold;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        .bg-gradient-primary {
            background-image: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="bg-gradient-primary text-white py-20 md:py-32 px-4">
        <div class="max-w-6xl mx-auto">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-6">Spring 事务传播行为</h1>
                    <p class="text-xl md:text-2xl opacity-90 mb-8">掌控事务边界，确保数据一致性</p>
                    <div class="flex space-x-4">
                        <a href="#intro" class="px-6 py-3 bg-white text-indigo-700 font-medium rounded-lg hover:bg-gray-100 transition duration-300">
                            <i class="fas fa-book-open mr-2"></i>开始阅读
                        </a>
                        <a href="#types" class="px-6 py-3 border-2 border-white text-white font-medium rounded-lg hover:bg-white hover:bg-opacity-10 transition duration-300">
                            <i class="fas fa-list-ul mr-2"></i>传播类型
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="mermaid">
                        graph LR
                            A[调用方法] --> B{是否存在事务?}
                            B -->|是| C[REQUIRED: 加入当前事务]
                            B -->|否| D[REQUIRED: 创建新事务]
                            C --> E[业务逻辑]
                            D --> E
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-4 py-16">
        <!-- Introduction Section -->
        <section id="intro" class="mb-20">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b-2 border-indigo-200 pb-2">事务传播行为概述</h2>
            <p class="drop-cap text-lg text-gray-700 mb-6 leading-relaxed">Spring 事务传播行为是 Spring 框架中的一个重要概念，用于定义在事务方法调用时，当前事务的传播规则。它决定了一个事务方法在调用另一个事务方法时的行为，以及如何处理不同事务之间的关系。事务传播行为对于确保数据的一致性和可靠性非常重要。</p>
            
            <div class="grid md:grid-cols-3 gap-6 mt-10">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-indigo-600 mb-4">
                        <i class="fas fa-project-diagram text-4xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">定义事务边界</h3>
                    <p class="text-gray-600">控制了事务的边界，决定是否使用现有事务或创建新事务，确保数据操作的一致性和完整性。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-indigo-600 mb-4">
                        <i class="fas fa-database text-4xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">确保数据一致性</h3>
                    <p class="text-gray-600">在多层调用中确保所有数据变化要么全部成功，要么全部回滚，保持数据的一致性。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-indigo-600 mb-4">
                        <i class="fas fa-sliders-h text-4xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">提高事务灵活性</h3>
                    <p class="text-gray-600">根据业务需求灵活配置事务，决定方法是否需要新事务或在现有事务中执行。</p>
                </div>
            </div>
        </section>

        <!-- Types Section -->
        <section id="types" class="mb-20">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b-2 border-indigo-200 pb-2">Spring 事务传播行为类型</h2>
            <p class="text-lg text-gray-700 mb-10 leading-relaxed">Spring 提供了多种事务传播行为，每种行为都有特定的应用场景和作用。以下是最常用的七种传播行为：</p>
            
            <div class="space-y-8">
                <!-- REQUIRED -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-link"></i>
                            </div>
                            <h3 class="text-2xl font-semibold">REQUIRED（默认）</h3>
                        </div>
                        <div class="ml-14">
                            <div class="bg-gray-100 p-4 rounded-lg mb-4">
                                <code class="text-sm text-gray-800">
                                    @Transactional(propagation = Propagation.REQUIRED)<br>
                                    public void performOperation() {<br>
                                    &nbsp;&nbsp;// 业务逻辑<br>
                                    }
                                </code>
                            </div>
                            <div class="flex flex-col md:flex-row md:space-x-6">
                                <div class="mb-4 md:mb-0">
                                    <h4 class="font-medium text-gray-900 mb-2">行为</h4>
                                    <p class="text-gray-600">如果当前存在事务，则加入当前事务；如果没有事务，则创建一个新的事务。</p>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-2">应用场景</h4>
                                    <p class="text-gray-600">适用于大多数业务操作，确保所有相关操作在一个事务中完成。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- REQUIRES_NEW -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-plus-circle"></i>
                            </div>
                            <h3 class="text-2xl font-semibold">REQUIRES_NEW</h3>
                        </div>
                        <div class="ml-14">
                            <div class="bg-gray-100 p-4 rounded-lg mb-4">
                                <code class="text-sm text-gray-800">
                                    @Transactional(propagation = Propagation.REQUIRES_NEW)<br>
                                    public void performIndependentOperation() {<br>
                                    &nbsp;&nbsp;// 业务逻辑<br>
                                    }
                                </code>
                            </div>
                            <div class="flex flex-col md:flex-row md:space-x-6">
                                <div class="mb-4 md:mb-0">
                                    <h4 class="font-medium text-gray-900 mb-2">行为</h4>
                                    <p class="text-gray-600">总是创建一个新的事务。如果当前存在事务，则挂起当前事务，待新事务完成后再恢复。</p>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-2">应用场景</h4>
                                    <p class="text-gray-600">当需要确保某个操作独立于当前事务时使用，例如，记录日志或异步操作。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- NESTED -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-layer-group"></i>
                            </div>
                            <h3 class="text-2xl font-semibold">NESTED</h3>
                        </div>
                        <div class="ml-14">
                            <div class="bg-gray-100 p-4 rounded-lg mb-4">
                                <code class="text-sm text-gray-800">
                                    @Transactional(propagation = Propagation.NESTED)<br>
                                    public void performNestedOperation() {<br>
                                    &nbsp;&nbsp;// 业务逻辑<br>
                                    }
                                </code>
                            </div>
                            <div class="flex flex-col md:flex-row md:space-x-6">
                                <div class="mb-4 md:mb-0">
                                    <h4 class="font-medium text-gray-900 mb-2">行为</h4>
                                    <p class="text-gray-600">如果当前存在事务，则在当前事务中嵌套一个新的事务（使用 savepoint）。如果嵌套事务失败，只有嵌套事务会回滚，外部事务不受影响。</p>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-2">应用场景</h4>
                                    <p class="text-gray-600">适用于需要支持部分提交的业务操作，如复杂的分步骤业务流程。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- SUPPORTS -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-question-circle"></i>
                            </div>
                            <h3 class="text-2xl font-semibold">SUPPORTS</h3>
                        </div>
                        <div class="ml-14">
                            <div class="bg-gray-100 p-4 rounded-lg mb-4">
                                <code class="text-sm text-gray-800">
                                    @Transactional(propagation = Propagation.SUPPORTS)<br>
                                    public void performOptionalOperation() {<br>
                                    &nbsp;&nbsp;// 业务逻辑<br>
                                    }
                                </code>
                            </div>
                            <div class="flex flex-col md:flex-row md:space-x-6">
                                <div class="mb-4 md:mb-0">
                                    <h4 class="font-medium text-gray-900 mb-2">行为</h4>
                                    <p class="text-gray-600">如果当前存在事务，则加入当前事务；如果没有事务，则以非事务方式执行。</p>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-2">应用场景</h4>
                                    <p class="text-gray-600">当某些操作可以在事务内执行，也可以在非事务状态下执行时使用。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- NOT_SUPPORTED -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-ban"></i>
                            </div>
                            <h3 class="text-2xl font-semibold">NOT_SUPPORTED</h3>
                        </div>
                        <div class="ml-14">
                            <div class="bg-gray-100 p-4 rounded-lg mb-4">
                                <code class="text-sm text-gray-800">
                                    @Transactional(propagation = Propagation.NOT_SUPPORTED)<br>
                                    public void performNonTransactionalOperation() {<br>
                                    &nbsp;&nbsp;// 业务逻辑<br>
                                    }
                                </code>
                            </div>
                            <div class="flex flex-col md:flex-row md:space-x-6">
                                <div class="mb-4 md:mb-0">
                                    <h4 class="font-medium text-gray-900 mb-2">行为</h4>
                                    <p class="text-gray-600">总是以非事务方式执行。如果当前存在事务，则挂起当前事务。</p>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-2">应用场景</h4>
                                    <p class="text-gray-600">当某些操作不需要事务支持时使用，例如只读查询操作。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- MANDATORY -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-exclamation-circle"></i>
                            </div>
                            <h3 class="text-2xl font-semibold">MANDATORY</h3>
                        </div>
                        <div class="ml-14">
                            <div class="bg-gray-100 p-4 rounded-lg mb-4">
                                <code class="text-sm text-gray-800">
                                    @Transactional(propagation = Propagation.MANDATORY)<br>
                                    public void performMandatoryOperation() {<br>
                                    &nbsp;&nbsp;// 业务逻辑<br>
                                    }
                                </code>
                            </div>
                            <div class="flex flex-col md:flex-row md:space-x-6">
                                <div class="mb-4 md:mb-0">
                                    <h4 class="font-medium text-gray-900 mb-2">行为</h4>
                                    <p class="text-gray-600">如果当前存在事务，则加入当前事务；如果没有事务，则抛出异常。</p>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-2">应用场景</h4>
                                    <p class="text-gray-600">当某个操作必须在事务中执行时使用，适用于业务逻辑需要强制事务支持的场景。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- NEVER -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">
                                <i class="fas fa-times-circle"></i>
                            </div>
                            <h3 class="text-2xl font-semibold">NEVER</h3>
                        </div>
                        <div class="ml-14">
                            <div class="bg-gray-100 p-4 rounded-lg mb-4">
                                <code class="text-sm text-gray-800">
                                    @Transactional(propagation = Propagation.NEVER)<br>
                                    public void performNeverTransactionalOperation() {<br>
                                    &nbsp;&nbsp;// 业务逻辑<br>
                                    }
                                </code>
                            </div>
                            <div class="flex flex-col md:flex-row md:space-x-6">
                                <div class="mb-4 md:mb-0">
                                    <h4 class="font-medium text-gray-900 mb-2">行为</h4>
                                    <p class="text-gray-600">总是以非事务方式执行。如果当前存在事务，则抛出异常。</p>
                                </div>
                                <div>
                                    <h4 class="font-medium text-gray-900 mb-2">应用场景</h4>
                                    <p class="text-gray-600">当某个操作不允许在事务中执行时使用，适用于某些不允许事务干扰的操作。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b-2 border-indigo-200 pb-2">事务传播行为流程图</h2>
            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="mermaid">
                    flowchart TD
                        A[调用方法] --> B{事务传播类型}
                        B -->|REQUIRED| C{存在当前事务?}
                        C -->|是| D[加入当前事务]
                        C -->|否| E[创建新事务]
                        B -->|REQUIRES_NEW| F[创建新事务\n挂起当前事务]
                        B -->|NESTED| G{存在当前事务?}
                        G -->|是| H[创建嵌套事务]
                        G -->|否| I[创建新事务]
                        B -->|SUPPORTS| J{存在当前事务?}
                        J -->|是| K[加入当前事务]
                        J -->|否| L[非事务执行]
                        B -->|NOT_SUPPORTED| M[非事务执行\n挂起当前事务]
                        B -->|MANDATORY| N{存在当前事务?}
                        N -->|是| O[加入当前事务]
                        N -->|否| P[抛出异常]
                        B -->|NEVER| Q{存在当前事务?}
                        Q -->|是| R[抛出异常]
                        Q -->|否| S[非事务执行]
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-10">
        <div class="max-w-6xl mx-auto px-4">
            <div class="text-center">
                <p class="text-lg font-medium mb-2">技术小馆</p>
                <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition duration-300">
                    <i class="fas fa-globe mr-2"></i>http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>